<?
/*
		topGames: an iRC games bot
		Copyright (C) 2008 Marcos Garc�a <marcosgdf@gmail.com>
    
		This file is part of topGames

		This program is free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		any later version.

		This program is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.

		You should have received a copy of the GNU General Public License
		along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class events {
	function action($tg,$data) {
		if (is_array($data) && (count($data) > 1)) {
			$command = strtolower($data[1]);
			if ($command == "quit") $this->quit($tg,$data[0]);
			elseif ($command == "part") $this->part($tg,$data[0],$data[2]);
			elseif ($command == "join") $this->join($tg,$data[0],$data[2]);
			elseif ($command == "kick") $this->kick($tg,$data,$data[2]);
			elseif ($command == "nick") $this->nick($tg,$data[0],$data[2]);
		}
	}
	function quit($tg,$data) {
		$explode = explode("!", $data);
		$nick = str_replace(":", "", $explode[0]);
		$tg->dbSend("DELETE FROM ".DB_PREFIX."channelusers WHERE `user`='$nick' AND `bot_number`='".BOT_NUMBER."'");
	}
	function part($tg,$data,$channel) {
		$explode = explode("!", $data);
		$nick = str_replace(":", "", $explode[0]);
		$channel = str_replace(":","", $channel);
		if ($nick != IRC_NICK) $tg->dbSend("DELETE FROM ".DB_PREFIX."channelusers WHERE `user`='$nick' AND `channel`='$channel' AND `bot_number`='".BOT_NUMBER."'");
	}
	function join($tg,$data,$channel) {
		$explode = explode("!", $data);
		$nick = str_replace(":", "", $explode[0]);
		$channel = str_replace(":","", $channel);
		if ($nick != IRC_NICK) {
			$tg->dbSend("INSERT INTO ".DB_PREFIX."channelusers SET `channel`='$channel', `user`='$nick', `bot_number`='".BOT_NUMBER."'");
			$query = $tg->dbSend("SELECT gid FROM ".DB_PREFIX."users WHERE `nick`='$nick'");
			$result = mysql_result($query,0);
			if ($result != 0) {
				switch($result) {
					case 2:
					$tg->ircMsg($channel,'Bienvenido '.$tg->h($nick).', recuerda que eres '.$tg->c(10,$tg->b($tg->u('pRE-oPERADOR'))).' de '.FULL_LOGO, true);
					break;
					case 3:
					$tg->ircMsg($channel,'Bienvenido '.$tg->h($nick).', recuerda que eres '.$tg->c(4,$tg->b($tg->u('oPERADOR'))).' de '.FULL_LOGO, true);
					break;
					case 4:
					$tg->ircMsg($channel,'Bienvenido '.$tg->h($nick).', recuerda que eres '.$tg->c(3,$tg->b($tg->u('CO-aDMINISTRADOR'))).' de '.FULL_LOGO, true);
					break;
					case 5:
					$tg->ircMsg($channel,'Bienvenido '.$tg->h($nick).', recuerda que eres '.$tg->c(6,$tg->b($tg->u('aDMINISTRADOR'))).' de '.FULL_LOGO, true);
					break;
				}
			}
		}
	}
	function kick($tg,$data,$channel) {
		$explode = explode("!", $data[0]);
		$nick = str_replace(":", "", $explode[0]);
		$channel = str_replace(":","", $channel);
		if ($nick == IRC_NICK) {
			global $_CFG;
			$tg->joinChannel($canal);
			$reason = str_replace(":", "", $data[4]);
			$tg->ircMsg(CHANNEL, LOGO."El usuario $nick me ha kickeado de $channel con la razon $reason");
			$tg->dbSend("DELETE FROM ".DB_PREFIX."channelusers WHERE `channel`='$channel");
			if (LOG_STATUS && (in_array('channels',$_CFG["LOG_REC"]))) $tg->writeChannelLog($channel,'kicked');
		}
		else $tg->dbSend("DELETE FROM ".DB_PREFIX."channelusers WHERE `user`='$nick' AND `channel`='$channel' AND `bot_number`='".BOT_NUMBER."'");
	}
	function nick($tg,$data,$nick) {
		$explode = explode("!", $data);
		$oldNick = str_replace(":", "", $explode[0]);
		$newNick = str_replace(":", "", $nick);
		$tg->dbSend("UPDATE ".DB_PREFIX."channelusers SET `user`='$newNick' WHERE `user`='$oldNick' AND `bot_number`='".BOT_NUMBER."'");
	}
}
?>